#Set working directory
setwd("Working_Directory_Here")
#Load conjoint analysis package
library(cregg)
#Load data
load("JLC_KrewOwens.RData")

#Make "originalism" the reference category for judicial philosophy
long$Philosophy <- relevel(long$Philosophy,ref="Originalism")

#C.1 of Appendix
plot(mm(long,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
        weights=~weight))

#C.2 of Appendix
plot(mm(long,Score~Sex+Race+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
        weights=~weight))

#Electing states
long1 <- long[long$Elect==1 & long$Selection!="DC",]
#Appointing states
long2 <- long[long$Elect==0 & long$Selection!="DC",]

#Calculate marginal means (electing states)
mm(long1,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)

#Calculate marginal means (appointing states)
mm(long2,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)

#Estimate AMCES (electing states)
cregg::amce(long1,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)

#Estimate AMCES (appointing states)
cregg::amce(long2,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)

#Estimate differences in AMCES by state judicial selection method)
amce_diffs(long,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
           by=~Elect,weights=~weight)

#Older individuals only (age above mean)
long1 <- long[long$Elect==1 & long$agebin==1 & long$Selection!="DC",]
long2 <- long[long$Elect==0 & long$agebin==1 & long$Selection!="DC",]
long3 <- long[long$agebin==1 & long$Selection!="DC",]
mm(long1,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
mm(long2,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
cregg::amce(long1,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
cregg::amce(long2,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
amce_diffs(long3,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
           by=~Elect,weights=~weight)

#Younger individuals
long1 <- long[long$Elect==1 & long$agebin==0 & long$Selection!="DC",]
long2 <- long[long$Elect==0 & long$agebin==0 & long$Selection!="DC",]
long3 <- long[long$agebin==0 & long$Selection!="DC",]
mm(long1,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
mm(long2,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
cregg::amce(long1,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
cregg::amce(long2,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
amce_diffs(long3,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
           by=~Elect,weights=~weight)

#Break out results by party identification
long1 <- long[long$pid3=="Democrat" & long$Elect==0 & long$Selection!="DC",]
long2 <- long[long$pid3=="Democrat" & long$Elect==1 & long$Selection!="DC",]
long3 <- long[long$pid3=="Independent" & long$Elect==0 & long$Selection!="DC",]
long4 <- long[long$pid3=="Independent" & long$Elect==1 & long$Selection!="DC",]
long5 <- long[long$pid3=="Republican" & long$Elect==0 & long$Selection!="DC",]
long6 <- long[long$pid3=="Republican" & long$Elect==1 & long$Selection!="DC",]
long7 <- long[long$pid3=="Democrat" & long$Selection!="DC",]
long8 <- long[long$pid3=="Independent" & long$Selection!="DC",]
long9 <- long[long$pid3=="Republican" & long$Selection!="DC",]
mm(long1,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
mm(long2,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
mm(long3,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
mm(long4,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
mm(long5,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
mm(long6,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
   weights=~weight)
cregg::amce(long1,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
cregg::amce(long2,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
cregg::amce(long3,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
cregg::amce(long4,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
cregg::amce(long5,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
cregg::amce(long6,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
            weights=~weight)
amce_diffs(long7,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
           by=~Elect,weights=~weight)
amce_diffs(long8,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
           by=~Elect,weights=~weight)
amce_diffs(long9,Score~Age+Sex+Race+Religion+Legal_Education+Position+Philosophy+Rating+Held_Office+Appt_Party+Ideology,
           by=~Elect,weights=~weight)

#Setup for omnibus F-tests
long <- long[long$pid3=="Republican" | long$pid3=="Democrat" | long$pid3=="Independent",]
long <- long[long$Selection!="DC",]
#See Footnote 19
mod1 <- lm(Score~(Philosophy+Elect)*pid3,long,weights=weight)
mod2 <- lm(Score~Philosophy*Elect*pid3,long,weights=weight)
anova(mod1,mod2)
#See Footnote 20
mod1 <- lm(Score~Age+Race+Religion+Legal_Education+Position+Held_Office+Ideology+Rating+(Philosophy+Sex+Appt_Party+Elect)*pid3,long,weights=weight)
mod2 <- lm(Score~(Age+Race+Religion+Legal_Education+Position+Held_Office+Ideology+Rating+(Philosophy+Sex+Appt_Party+Elect)*pid3)*Elect,long,weights=weight)
anova(mod1,mod2)

